package com.xiaohua.xiaohuamall.product.service.impl;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohua.common.utils.PageUtils;
import com.xiaohua.common.utils.Query;

import com.xiaohua.xiaohuamall.product.dao.SkuInfoDao;
import com.xiaohua.xiaohuamall.product.entity.SkuInfoEntity;
import com.xiaohua.xiaohuamall.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        /**
         * key:
         * catelogId:0
         * brandId:0
         * min:0
         * max:0
         */
        if (!StringUtils.isEmpty(params.get("key"))){
            wrapper.and(w->{
                w.eq("sku_id",params.get("key")).or().like("sku_name",params.get("key"));
            });
        }
        if (!StringUtils.isEmpty(params.get("catelogId")) && !"0".equalsIgnoreCase((String) params.get("catelogId"))){
            wrapper.eq("catalog_id",params.get("catelogId"));
        }
        if (!StringUtils.isEmpty(params.get("brandId")) && !"0".equalsIgnoreCase((String) params.get("brandId"))){
            wrapper.eq("brand_id",params.get("brandId"));
        }
        if (!StringUtils.isEmpty(params.get("min"))){
            wrapper.ge("price",params.get("min"));
        }
        if (!StringUtils.isEmpty(params.get("max"))){
            try {
                BigDecimal bigDecimal = new BigDecimal((String) params.get("max"));
                if (bigDecimal.compareTo(new BigDecimal("0")) == 1){
                    wrapper.le("price",params.get("max"));
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> list = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
        return list;
    }

}